package com.justgame.project.service.art.service;


import com.justgame.project.cloud.common.exception.Exc;
import com.justgame.project.cloud.common.util.ObjUtil;
import com.justgame.project.cloud.common.entity.PageRequest;
import com.justgame.project.service.api.dto.ArtPageCategoryDTO;
import com.justgame.project.service.api.vo.ArtPageCategoryVO;
import com.justgame.project.service.art.mapper.ArtPageCategoryMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Data
@Slf4j
@Service
@Transactional
@AllArgsConstructor
public class ArtPageCategoryService {
    private final ArtPageCategoryMapper artPageCategoryMapper;

    public PageRequest<ArtPageCategoryVO> getCategoryList(ArtPageCategoryDTO dto, PageRequest pageRequest) {
        pageRequest.startPage();
        return pageRequest.BuildPageInfo(artPageCategoryMapper.Select(dto));
    }

    @Cacheable(cacheNames = {"ArtPageCategory"} )
    public boolean delete(List<String> ids) {
        if (ids != null && ids.size() > 0) {
            return artPageCategoryMapper.Delete(ids) > 0;
        } else {
            return false;
        }
    }

    @Cacheable(cacheNames = {"ArtPageCategory"} )
    public boolean addPageType(ArtPageCategoryDTO dto) {
        ObjUtil.inject(dto, true);
        artPageCategoryMapper.Insert(dto);
        return true;
    }

    public boolean updateById(ArtPageCategoryDTO dto) {
        if (!ObjUtil.checkId(dto)) throw new Exc("请重新选择后再试！");
        return artPageCategoryMapper.UpdateById(dto) > 0;
    }

    @Cacheable(cacheNames = {"ArtPageCategory"} ,key = "'AllCategoryIds'")
    public List<String> getAllCategoryIds(){
        return artPageCategoryMapper.Select(new ArtPageCategoryDTO()).stream().map(ArtPageCategoryVO::getId).collect(Collectors.toList());
    }

}
